<?php
namespace app\api\controller;
use think\Db;
use app\admin\model\Config;
use app\common\model\School as SchoolModel;
use app\common\service\GuguService;

//院校对比
class SchoolCompare extends App
{
    
    //加入
    public function add(){
        $user_id = $this->user_id;
        if($this->request->isPost()){
			
			$school_id = $this->request->post("school_id",0,'intval');
			if(empty($school_id)){
				return $this->show(2001,"缺少参数school_id!", '');
			}
			$flag = Db::name("school_compare")->where('user_id',$user_id)->where('school_id',$school_id)->value('id');
			$data['school_id'] = $school_id;
			$data['user_id'] = $user_id;
			if(empty($flag)){
				$data['create_time'] = time();
				$res = Db::name("school_compare")->insert($data);
			}else{
				$data['update_time'] = time();
				$res = Db::name("school_compare")->where('id',$flag)->update($data);
			}
			if( $res !== false ){
				return $this->show(1,"success", '');
			}else{
				return $this->show(2002, "fail", "");
			} 
		}else{
            return $this->show(2001, "非法请求", "");
        }
    }
    
    public function del()
    {
        
        $user_id = $this->user_id;
        $school_id = $this->request->get("school_id",0,'intval'); 
		if(empty($school_id)){
			return $this->show(2001,"缺少参数school_id!", '');
		}
		
		$flag = Db::name("school_compare")->where('user_id',$user_id)->where('school_id',$school_id)->delete();
		return $this->show(1,"success", '');
		
    }
    
    //院校对比列表
    public function index()
    {
        $user_id = $this->user_id;
		$school_ids = Db::name("school_compare")->where('user_id',$user_id)->column('school_id');
		$map['id'] = ['in',$school_ids];
		$list = Db::name("school")->where($map)->field('id,college_name,cover_image,hot_nums,difficult_nums,province_id,city_id,xian_id,school_suoshu,ranking,school_type,school_xingzhi')->order('ranking asc,id desc')->select(); 
		
		
		$suoshu_list = SchoolModel::getSuoshuList();	
		foreach($list as $k=>$v){
			$list[$k]['province'] = Db::name('area')->where('id',$v['province_id'])->value('name');
			$list[$k]['city'] = Db::name('area')->where('id',$v['city_id'])->value('name');
			$list[$k]['district'] = Db::name('area')->where('id',$v['xian_id'])->value('name');
			//$list[$k]['school_type'] = Db::name('school_type_data')->alias('d')->join('school_type t','d.type_id=t.id')->where('d.school_id',$v['id'])->value('t.title');
			$list[$k]['school_type'] = Db::name('school_type')->where('id',$v['school_type'])->value('title');
			 
			//$list[$k]['school_xingzhi'] = Db::name('school_xingzhi_data')->alias('d')->join('school_xingzhi x','d.xingzhi_id=x.id')->where('d.school_id',$v['id'])->value('x.title');
			$list[$k]['school_xingzhi'] = Db::name('school_xingzhi')->where('id',$v['school_xingzhi'])->value('title');
			
			$list[$k]['school_suoshu'] = !empty($suoshu_list[$v['school_suoshu']])?$suoshu_list[$v['school_suoshu']]:'';
			$list[$k]['school_tese'] = Db::name('school_tese_data')->alias('d')->join('school_tese t','d.tese_id=t.id')->where('d.school_id',$v['id'])->column('t.title');
			
		} 
		return $this->show(1,"成功", $list);
		
    }
	
	//pk
    public function pk()
    {
        $user_id = $this->user_id;
		$school_ids = $this->request->get("school_ids",0,'trim');
		if(empty($school_ids)){
			return $this->show(2001,"缺少参数school_ids!", '');
		}
		$map['id'] = ['in',$school_ids];
		$list = Db::name("school")->where($map)->field('id,college_name,cover_image,hot_nums,difficult_nums,province_id,city_id,xian_id,school_suoshu,ranking,shuoshi_count,boshi_count,pinggu,school_type,school_xingzhi')->order('ranking asc,id desc')->select(); 
		if(count($list)>6){
			return $this->show(2001,"一次最多只能选6所院校!", '');
		}
		$year = date('Y',strtotime("-1 year"));
		$province_id = $this->province_id;
		$enrollprovince = Db::name("area")->where('id',$province_id)->value('name');
		$suoshu_list = SchoolModel::getSuoshuList();	
		foreach($list as $k=>$v){
			$list[$k]['province'] = Db::name('area')->where('id',$v['province_id'])->value('name');
			$list[$k]['city'] = Db::name('area')->where('id',$v['city_id'])->value('name');
			$list[$k]['district'] = Db::name('area')->where('id',$v['xian_id'])->value('name');
			//$list[$k]['school_type'] = Db::name('school_type_data')->alias('d')->join('school_type t','d.type_id=t.id')->where('d.school_id',$v['id'])->value('t.title');
			$list[$k]['school_type'] = Db::name('school_type')->where('id',$v['school_type'])->value('title');
			 
			//$list[$k]['school_xingzhi'] = Db::name('school_xingzhi_data')->alias('d')->join('school_xingzhi x','d.xingzhi_id=x.id')->where('d.school_id',$v['id'])->value('x.title');
			$list[$k]['school_xingzhi'] = Db::name('school_xingzhi')->where('id',$v['school_xingzhi'])->value('title');
			
			$list[$k]['school_suoshu'] = !empty($suoshu_list[$v['school_suoshu']])?$suoshu_list[$v['school_suoshu']]:'';
			$list[$k]['school_tese'] = Db::name('school_tese_data')->alias('d')->join('school_tese t','d.tese_id=t.id')->where('d.school_id',$v['id'])->column('t.title');
			/*if(!empty($v['pinggu'])){
				$pinggu = array_filter(json_decode($v['pinggu'],true));
				$pinggu_result_new = [];
				$pinggu_result = array_unique(array_column($pinggu,'result'));
				foreach($pinggu_result as $ka=>$val){
					$pinggu_result_new[$ka]['level'] = $val;
					$pinggu_result_new[$ka]['data'] = [];
					foreach($pinggu as $vp){
						if($val == $vp['result']){
							array_push($pinggu_result_new[$ka]['data'],$vp['title']);
						}
					}
					$pinggu_result_new[$ka]['count'] = count($pinggu_result_new[$ka]['data']);
				}
				//dump($pinggu_result_new);die;
				$pinggu = ['count'=>count($pinggu),'data'=>$pinggu_result_new];
			}else{
				$pinggu = '';
			}*/
			if(!empty($v['pinggu'])){
				$pinggu = array_filter(json_decode($v['pinggu'],true));
			}else{
				$pinggu = '';
			}
			$list[$k]['pinggu'] = $pinggu;
			
			
			/*//专业 ceemajorline
			$result = GuguService::make('/metadata/ceemajorline',['appkey'=>'TZK6DJZ5LVS6V8LYDAS9WE3K4TM8ZQWZ','pageindex'=>1,'pagesize'=>100,'enrollprovince'=>$enrollprovince,'schoolname'=>$v['college_name'],'year'=>$year]);*/
			//院校 
			$result = GuguService::make('/metadata/ceecollegeline',['appkey'=>'UZ8X3YJWLFJWJKY28U63K8YFUTQZX4RD','pageindex'=>1,'pagesize'=>100,'searchtype'=>'COLLEGENAME','keyword'=>$v['college_name'],'year'=>$year,'enrollprovince'=>$enrollprovince]);
			if($result[0] != 200){
				return $this->show(2002,$result[1]??'error', '');
			}
			$ceecollegeline = $result[1][1];
			$LowestScores = array_column($ceecollegeline,'LowestScore');
			if(!empty($LowestScores)){
				$list[$k]['lowest_scores_bw'] = min($LowestScores).'-'.max($LowestScores);
			}else{
				$list[$k]['lowest_scores_bw'] = ''.'-'.'';
			}
			

		} 
		return $this->show(1,"成功", $list);
		
    }
    
}